package atdd.kata.core.controller;


import atdd.kata.core.dao.api.PersonDao;
import atdd.kata.core.domain.Person;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;


@Controller
public class PersonCRUDController {

    @Autowired
    PersonDao personDao;

    @RequestMapping(value="/person/create", method = RequestMethod.POST)
    public String createNewPerson(@ModelAttribute Person person) {

        personDao.create(person);

        return "redirect:list";
    }

    @RequestMapping(value="/person/create", method = RequestMethod.GET)
    public String returnCreateFrom(HttpServletRequest request) {

        request.setAttribute("createPerson", new Person());

        return "create_person";
    }

    @RequestMapping(value="/person/list", method = RequestMethod.GET)
    public String showAllPersons(HttpServletRequest request) {

        List<Person> persons = personDao.getAllPersons();

        request.setAttribute("personList", persons);

        return "person_list";
    }
}
